/*
 * @Description:管理端数据接口
 * @Author: YaoXiaotTian
 * @Date: 2021-05-15 20:00:12
 * @LastEditors: YaoXiaoTian
 * @LastEditTime: 2021-05-19 17:09:14
 */
const { json } = require("express");
const { query } = require("./mysql");
/**
 * 获取管理端的所有列表数据
 * source
 *     1    管理员
 *     2    用户
 *     3    管理员发布的文章
 *     4    用户的评论数据
 *     5    系统消息
 *     6    举报消息
 *
 */
exports.getList = (req, res) => {
  const { source, pageNo } = req.query;
  let sql = "";
  let sql1 = "";
  let offset = (Number.parseInt(pageNo) - 1) * 10;
  switch (Number.parseInt(source)) {
    case 1:
      sql = `select * from user_information where type = 2 limit ${offset},10`;
      sql1 = `select count(1) as totalNum from user_information where type = 2`;
      break;
    case 2:
      sql = `select * from user_information where type = 1 limit ${offset},10`;
      sql1 = `select count(1) as totalNum from user_information where type = 1`;
      break;
    case 3:
      sql = `select * from entertainment_introduction limit ${offset},10`;
      sql1 = `select count(1) as totalNum from entertainment_introduction`;
      break;
    case 4:
      sql = `select A.*,B.title as articleTitle,C.nickName as nickName,C.account as account from comment_data A,article_data B,user_information C where A.articleId = B.id and A.userId = C.userId limit ${offset},10`;
      sql1 = `select count(1) as totalNum from comment_data`;
      break;
    case 5:
      sql = `select A.*,B.nickName as nickName from system_info A,user_information B where A.userId = B.userId  limit ${offset},10`;
      sql1 = `select count(1) as totalNum from system_info`;
      break;
    case 6:
      sql = `select * from report_news limit ${offset},10`;
      sql1 = `select count(1) as totalNum from report_news`;
      break;
  }

  query(sql1, (err, result, fields) => {
    if (err) {
      res.json({
        code: -1,
        msg: err,
      });
      return;
    }
    query(sql, (err, result1) => {
      if (err) {
        res.json({
          code: -1,
          msg: err,
        });
        return;
      }
      let data = JSON.parse(JSON.stringify(result1));
      let totalNum = JSON.parse(JSON.stringify(result))[0].totalNum;
      res.send({
        code: 0,
        data: result1,
        totalNum,
        msg: "数据获取成功",
      });
    });
  });
};

// 管理端获取过滤的数据
exports.getFilterData = (req, res) => {
  const { source, pageNo, ...params } = req.query;
  let sql = "";
  let sql1 = "";
  let offset = (Number.parseInt(pageNo) - 1) * 10;
  switch (Number.parseInt(source)) {
    case 1:
      if (params.nickName && !params.account) {
        sql = `select * from user_information where type = 2 and nickName like '%${params.nickName}%' limit ${offset},10`;
        sql1 = `select count(1) as totalNum from user_information where type = 2 and nickName like '%${params.nickName}%'`;
      } else if (params.account && !params.nickName) {
        sql = `select * from user_information where type = 2 and account = ${params.account} limit ${offset},10`;
        sql1 = `select count(1) as totalNum from user_information where type = 2 and account = ${params.account}`;
      } else {
        sql = `select * from user_information where type = 2 and account = ${params.account} and nickName like '%${params.nickName}%' limit ${offset},10`;
        sql1 = `select count(1) as totalNum from user_information where type = 2 and account = ${params.account} and nickName like '%${params.nickName}%'`;
      }
      break;
    case 2:
      if (params.nickName && !params.account) {
        sql = `select * from user_information where type = 1 and nickName like '%${params.nickName}%' limit ${offset},10`;
        sql1 = `select count(1) as totalNum from user_information where type = 1 and nickName like '%${params.nickName}%'`;
      } else if (params.account && !params.nickName) {
        sql = `select * from user_information where type = 1 and account = ${params.account} limit ${offset},10`;
        sql1 = `select count(1) as totalNum from user_information where type = 1 and account = ${params.account}`;
      } else {
        sql = `select * from user_information where type = 1 and account = ${params.account} and nickName like '%${params.nickName}%' limit ${offset},10`;
        sql1 = `select count(1) as totalNum from user_information where type = 1 and account = ${params.account} and nickName like '%${params.nickName}%'`;
      }
      break;
    case 3:
      sql = `select * from entertainment_introduction where title like '%${params.title}%' limit ${offset},10`;
      sql1 = `select count(1) as totalNum from entertainment_introduction where title like '%${params.title}%'`;
      break;
    case 4:
      if (params.nickName && !params.account) {
        sql = `select A.*,B.title as articleTitle,C.nickName as nickName,C.account as account from comment_data A,article_data B,user_information C where A.articleId = B.id and A.userId = C.userId and C.nickName like '%${params.nickName}%' limit ${offset},10`;
        sql1 = `select count(1) as totalNum from comment_data A,user_information C where A.userId = C.userId and C.nickName like '%${params.nickName}%'`;
      } else if (params.account && !params.nickName) {
        sql = `select A.*,B.title as articleTitle,C.nickName as nickName,C.account as account from comment_data A,article_data B,user_information C where A.articleId = B.id and A.userId = C.userId and C.account = ${params.account} limit ${offset},10`;
        sql1 = `select count(1) as totalNum from comment_data A,user_information C where A.userId = C.userId and C.account = ${params.account}`;
      } else {
        sql = `select A.*,B.title as articleTitle,C.nickName as nickName,C.account as account from comment_data A,article_data B,user_information C where A.articleId = B.id and A.userId = C.userId and C.account = ${params.account} and C.nickName like '%${params.nickName}%' limit ${offset},10`;
        sql1 = `select count(1) as totalNum from comment_data A,user_information C where A.userId = C.userId and C.account = ${params.account} and C.nickName like '%${params.nickName}%'`;
      }
      break;
    case 5:
      sql = `select A.*,B.nickName as nickName from system_info A,user_information B where A.userId = B.userId and A.title like '%${params.title}%' limit ${offset},10`;
      sql1 = `select count(1) as totalNum from system_info where title like '%${params.title}%'`;
      break;
    case 6:
      if (params.nickName && !params.articleTitle) {
        sql = `select * from report_news where nickName like '%${params.nickName}%' limit ${offset},10`;
        sql1 = `select count(1) as totalNum from report_news where nickName like '%${params.nickName}%'`;
      } else if (params.articleTitle && !params.nickName) {
        sql = `select * from report_news where articleTitle like '%${params.articleTitle}%' limit ${offset},10`;
        sql1 = `select count(1) as totalNum from report_news where articleTitle like '%${params.articleTitle}%'`;
      } else {
        sql = `select * from report_news where articleTitle like' %${params.articleTitle}%'  and nickName like '%${params.nickName}%' limit ${offset},10`;
        sql1 = `select count(1) as totalNum from report_news wherearticleTitle like '%${params.articleTitle}%'  and nickName like '%${params.nickName}%'`;
      }
      break;
  }
  query(sql1, (err, result, fields) => {
    if (err) {
      res.json({
        code: -1,
        msg: err,
      });
      return;
    }
    query(sql, (err, result1) => {
      if (err) {
        res.json({
          code: -1,
          msg: err,
        });
        return;
      }
      let totalNum = JSON.parse(JSON.stringify(result))[0].totalNum;
      res.send({
        code: 0,
        data: result1,
        totalNum,
        msg: "数据获取成功",
      });
    });
  });
};

//获取时间
function getCurrentTime() {
  let date = new Date();
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let strDate = date.getDate();
  let hour = date.getHours();
  let minute = date.getMinutes();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  if (hour >= 0 && hour <= 9) {
    hour = "0" + hour;
  }
  if (minute >= 0 && minute <= 9) {
    minute = "0" + minute;
  }
  let currentdate = `${year}-${month}-${strDate} ${hour}:${minute}`;
  return currentdate;
}
// 删除某条记录
exports.deleteRecord = (req, res) => {
  const { source, ...params } = req.query;
  let sql = "";
  switch (Number.parseInt(source)) {
    case 1:
      sql = `delete from user_information where type = 2 and userId = ${params.userId}`;
      break;
    case 2:
      sql = `delete from user_information where type = 1 and userId = ${params.userId}`;
      break;
    case 3:
      sql = `delete from entertainment_introduction where id = ${params.id}`;
      break;
    case 4:
      sql = `update comment_data set content = "此条评论已被删除" where id = ${params.reviewId}`;
      break;
    case 5:
      sql = `delete from system_info where id = ${params.id}`;
      break;
  }
  query(sql, (err, result) => {
    if (err) {
      res.json({
        code: -1,
        err,
      });
      return;
    }
    if (Number.parseInt(source) == 4) {
      let sql1 = `select title from article_data where id = ${params.articleId}`;
      query(sql1, (err, result1) => {
        if (err) {
          res.json({
            code: -1,
            err,
          });
          return;
        }
        let articleTitle = JSON.parse(JSON.stringify(result1))[0].title;
        let time = getCurrentTime();
        let title = "评论违规警告";
        let content = `您在文章《${articleTitle}》发表的评论“${params.content}”,经过管理员核实，确认违规。为营造一个良好的网站的环境，我们将强制删除此条评论，特此警告。`;
        let sql3 = `insert into system_info (userId,content,title,time) values (?,?,?,?) `;
        query(sql3, [params.userId, content, title, time], (err, result3) => {
          if (err) {
            res.json({
              code: -1,
              err,
            });
            return;
          }
          res.json({
            code: 0,
            msg: "数据删除成功",
          });
        });
      });
    } else {
      res.json({
        code: 0,
        msg: "数据删除成功",
      });
    }
  });
};

// 新增、编辑管理员
exports.editAdministrator = (req, res) => {
  const {
    nickName,
    account,
    sex,
    password,
    introduction,
    profession,
    residence,
    personal_profile,
    userId,
  } = req.body;
  let sql = "";
  let data = [];
  if (!userId) {
    data = [
      nickName,
      account,
      sex,
      password,
      introduction,
      profession,
      residence,
      personal_profile,
      2,
    ];
    sql =
      "insert into user_information (nickName,account,sex,password,introduction,profession,residence,personal_profile,type) values (?,?,?,?,?,?,?,?,?)";
  } else {
    data = [
      nickName,
      account,
      sex,
      introduction,
      profession,
      residence,
      personal_profile,
    ];
    sql = `update user_information set nickName = ?,account = ?,sex = ?,introduction = ?,profession = ?,residence = ?,personal_profile = ? where userId = ${userId}`;
  }
  query(sql, data, (err) => {
    if (err) {
      res.json({
        code: -1,
        err,
      });
      return;
    }
    res.json({
      code: 0,
      msg: "操作成功",
    });
  });
};

// 新增用户
exports.addUser = (req, res) => {
  const { nickName, account, password } = req.body;
  let sql =
    "insert into user_information (nickName,account,password,type) values (?,?,?,?)";
  query(sql, [nickName, account, password, 1], (err) => {
    if (err) {
      res.json({
        code: -1,
        err,
      });
      return;
    }
    res.json({
      code: 0,
      msg: "用户增加成功",
    });
  });
};

// 管理员编辑、发布内容
exports.editContent = (req, res) => {
  const { title, type, img, intro, text, id } = req.body;
  let sql = "";
  let data = [title, type, img, intro, text];
  if (id) {
    sql = `update entertainment_introduction set title = ?,type = ?,img = ?,intro = ?,text = ? where id = ${id}`;
  } else {
    sql =
      "insert into entertainment_introduction (title, type, img, intro, text) values (?,?,?,?,?)";
  }
  query(sql, data, (err) => {
    if (err) {
      res.json({
        code: -1,
        err,
      });
      return;
    }
    res.json({
      code: 0,
      msg: "操作成功",
    });
  });
};
// 获取所有的用户列表
exports.getUsers = (req, res) => {
  let sql =
    "select nickName,userId,account from user_information where type = 1";
  query(sql, (err, result) => {
    if (err) {
      res.json({
        code: -1,
        msg: err,
      });
      return;
    }
    res.json({
      code: 0,
      data: result,
      msg: "数据获取成功",
    });
  });
};

// 发表系统消息
exports.publishSystem = (req, res) => {
  let { userId, content, time, title } = req.body;
  if (!time) {
    time = getCurrentTime();
  }
  let data = [];
  userId.split(",").forEach((item) => {
    data.push([Number.parseInt(item), content, time, title]);
  });
  let sql = "insert into system_info (userId, content, time, title) values ?";
  query(sql, [data], (err) => {
    if (err) {
      res.json({
        code: -1,
        err,
      });
      return;
    }
    res.json({
      code: 0,
      msg: "操作成功",
    });
  });
};

// 处理举报
exports.handleReport = (req, res) => {
  const { id, articleTitle, userId, handleResult } = req.body;
  let sql = `update report_news set isHandle = 1,handleResult = '${handleResult}' where id = ${id}`;
  let sql1 = `insert into system_info (userId, content, time, title) values (?,?,?,?)`;
  let content = `首先，感谢您为营造一个良好的网站所作出的贡献；其次，经我们管理员核实，您对文章《${articleTitle}》的举报${handleResult}。特此反馈`;
  let title = "举报反馈";
  let time = getCurrentTime();
  query(sql, (err) => {
    if (err) {
      res.json({
        code: -1,
        err,
      });
      return;
    }
    query(sql1, [userId, content, time, title], (err) => {
      if (err) {
        res.json({
          code: -1,
          err,
        });
        return;
      }
      res.json({
        code: 0,
        msg: "处理成功",
      });
    });
  });
};
